package com.haocang.szgm.ui.report.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.haocang.szgm.R;
import com.haocang.szgm.activity.comm.CommFinalActivity;
import com.haocang.szgm.requestcenter.DataModel;
import com.haocang.commonlib.network.interfaces.InitDataCallBack;
import com.haocang.commonlib.network.interfaces.InitDataOneBeanBack;
import com.haocang.szgm.ui.report.bean.PurificationPlantDayBean;
import com.haocang.szgm.utils.config.Config;
import com.haocang.szgm.utils.config.WebApi;
import com.haocang.commonlib.loadingdialog.LoadingDialog;
import com.haocang.szgm.utils.tools.CacheData;
import com.haocang.szgm.utils.tools.MyTextUtils;

import net.tsz.afinal.annotation.view.ViewInject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

/**
 * ===============================================
 * 类 名 称：PurificationPlantDayActivity
 * 创建时间：2019/4/12 14:05
 * 编 写 人：ShenC
 * 类 说 明：水质净化厂日报表
 * ================================================
 * 修改内容：      修改时间：      修改人：
 * ===============================================
 */
public class PurificationPlantDayEditActivity extends CommFinalActivity {

    @ViewInject(id = R.id.report_HandleAmount1)
    EditText report_HandleAmount1;
    @ViewInject(id = R.id.report_HandleAmount2)
    EditText report_HandleAmount2;
    @ViewInject(id = R.id.report_PumpLevel0)
    EditText report_PumpLevel0;
    @ViewInject(id = R.id.report_PumpLevel4)
    EditText report_PumpLevel4;
    @ViewInject(id = R.id.report_PumpLevel8)
    EditText report_PumpLevel8;
    @ViewInject(id = R.id.report_PumpLevel12)
    EditText report_PumpLevel12;
    @ViewInject(id = R.id.report_PumpLevel16)
    EditText report_PumpLevel16;
    @ViewInject(id = R.id.report_PumpLevel20)
    EditText report_PumpLevel20;

    @ViewInject(id = R.id.report_InletCOD)
    EditText report_InletCOD;
    @ViewInject(id = R.id.report_InletNH3)
    EditText report_InletNH3;
    @ViewInject(id = R.id.report_InletTN)
    EditText report_InletTN;
    @ViewInject(id = R.id.report_InletTP)
    EditText report_InletTP;
    @ViewInject(id = R.id.report_InletSS)
    EditText report_InletSS;
    @ViewInject(id = R.id.report_InletPH)
    EditText report_InletPH;


    @ViewInject(id = R.id.report_Effluent_1_COD)
    EditText report_Effluent_1_COD;
    @ViewInject(id = R.id.report_Effluent_1_NH3)
    EditText report_Effluent_1_NH3;
    @ViewInject(id = R.id.report_Effluent_1_TN)
    EditText report_Effluent_1_TN;
    @ViewInject(id = R.id.report_Effluent_1_TP)
    EditText report_Effluent_1_TP;
    @ViewInject(id = R.id.report_Effluent_1_SS)
    EditText report_Effluent_1_SS;
    @ViewInject(id = R.id.report_Effluent_1_PH)
    EditText report_Effluent_1_PH;

    @ViewInject(id = R.id.report_Effluent_2_COD)
    EditText report_Effluent_2_COD;
    @ViewInject(id = R.id.report_Effluent_2_NH3)
    EditText report_Effluent_2_NH3;
    @ViewInject(id = R.id.report_Effluent_2_TN)
    EditText report_Effluent_2_TN;
    @ViewInject(id = R.id.report_Effluent_2_TP)
    EditText report_Effluent_2_TP;
    @ViewInject(id = R.id.report_Effluent_2_SS)
    EditText report_Effluent_2_SS;
    @ViewInject(id = R.id.report_Effluent_2_PH)
    EditText report_Effluent_2_PH;


    @ViewInject(id = R.id.report_Effluent_1_Mud)
    EditText report_Effluent_1_Mud;
    @ViewInject(id = R.id.report_Effluent_1_MudWater)
    EditText report_Effluent_1_MudWater;
    @ViewInject(id = R.id.report_Effluent_2_Mud)
    EditText report_Effluent_2_Mud;
    @ViewInject(id = R.id.report_Effluent_2_MudWater)
    EditText report_Effluent_2_MudWater;

    @ViewInject(id = R.id.report_Memo)
    EditText report_Memo;
    @ViewInject(id = R.id.InletYL_ET)
    EditText InletYL_ET;
    @ViewInject(id = R.id.Effluent_1_YL_ET)
    EditText Effluent_1_YL_ET;


    @ViewInject(id = R.id.report_Creator)
    TextView report_Creator;
    @ViewInject(id = R.id.report_CreateDT)
    TextView report_CreateDT;

    @ViewInject(id = R.id.char_Keyboard)
    LinearLayout char_Keyboard;

    @ViewInject(id = R.id.submit)
    Button submit;

    private LoadingDialog dialog;

    private PurificationPlantDayBean model;
    private String BillID;

    private ArrayList<OnSoftKeyboardStateChangedListener> mKeyboardStateListeners = new ArrayList<>();      //软键盘状态监听列表
    private ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener;
    private boolean mIsSoftKeyboardShowing = false;


    public interface OnSoftKeyboardStateChangedListener {
        public void OnSoftKeyboardStateChanged(boolean isKeyBoardShow, int keyboardHeight);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initEvent();

        if (CacheData.PurificationPlantDay_UPDATE) {//阅读加载页面数据
            BillID = getIntent().getStringExtra("ID");
            initData();
        } else {
            report_Creator.setText(CacheData.UserName());
        }

    }


    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_ppreport_dayedit;
    }

    @Override
    protected boolean showNavigation() {
        return true;
    }

    @Override
    protected int showMoreIcon() {
        return 0;
    }

    @Override
    protected String showTitleName() {
        return "水质净化厂日报表";
    }


    private void initEvent() {

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
});

        report_CreateDT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  chooseTime();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initSubmit();
            }
        });


        mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                WindowManager manager = PurificationPlantDayEditActivity.this.getWindowManager();
                DisplayMetrics outMetrics = new DisplayMetrics();
                manager.getDefaultDisplay().getMetrics(outMetrics);
                int screenWidth = outMetrics.widthPixels;
                int screenHeight = outMetrics.heightPixels;

                //判断窗口可见区域大小
                Rect r = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //如果屏幕高度和Window可见区域高度差值大于整个屏幕高度的1/3，则表示软键盘显示中，否则软键盘为隐藏状态。
                int heightDifference = screenHeight - (r.bottom - r.top);
                boolean isKeyboardShowing = heightDifference > screenHeight / 3;

                //如果之前软键盘状态为显示，现在为关闭，或者之前为关闭，现在为显示，则表示软键盘的状态发生了改变
                if ((mIsSoftKeyboardShowing && !isKeyboardShowing) || (!mIsSoftKeyboardShowing && isKeyboardShowing)) {
                    mIsSoftKeyboardShowing = isKeyboardShowing;
                    LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) char_Keyboard.getLayoutParams();

                    if (mIsSoftKeyboardShowing) {
                        linearParams.height = heightDifference - 60;
                    } else {
                        linearParams.height = 0;
                    }
                    char_Keyboard.setLayoutParams(linearParams);

                    for (int i = 0; i < mKeyboardStateListeners.size(); i++) {
                        OnSoftKeyboardStateChangedListener listener = mKeyboardStateListeners.get(i);
                        listener.OnSoftKeyboardStateChanged(mIsSoftKeyboardShowing, heightDifference);
                    }
                }
            }
        };
        //注册布局变化监听
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(mLayoutChangeListener);
    }

    private void initData() {
        String ApiUrl = Config.host + WebApi.Api_GetPurificationPlantDay + "GetDetail?ID=" + BillID;
        dialog = new LoadingDialog(PurificationPlantDayEditActivity.this, "");
        dialog.show();
        DataModel.GetApiRequestOneMode("PurificationPlantDayBean", ApiUrl, new InitDataOneBeanBack<PurificationPlantDayBean>() {
            @Override
            public void callbak(boolean b, PurificationPlantDayBean data, String msg) {
                if (b) {
                    model = data;
                    //tv_name.setText("水质净化厂日报表("+MyTextUtils.toYMD(model.getCreateDT())+")");
                    report_HandleAmount1.setText(model.getHandleAmount1());
                    report_HandleAmount2.setText(model.getHandleAmount2());
                    report_PumpLevel0.setText(model.getPumpLevel0());
                    report_PumpLevel4.setText(model.getPumpLevel4());
                    report_PumpLevel8.setText(model.getPumpLevel8());
                    report_PumpLevel12.setText(model.getPumpLevel12());
                    report_PumpLevel16.setText(model.getPumpLevel16());
                    report_PumpLevel20.setText(model.getPumpLevel20());

                    report_InletCOD.setText(model.getInletCOD());
                    report_InletNH3.setText(model.getInletNH3());
                    report_InletTN.setText(model.getInletTN());
                    report_InletTP.setText(model.getInletTP());
                    report_InletSS.setText(model.getInletSS());
                    report_InletPH.setText(model.getInletPH());
                    report_Effluent_1_COD.setText(model.getEffluent_1_COD());
                    report_Effluent_1_NH3.setText(model.getEffluent_1_NH3());
                    report_Effluent_1_TN.setText(model.getEffluent_1_TN());
                    report_Effluent_1_TP.setText(model.getEffluent_1_TP());
                    report_Effluent_1_SS.setText(model.getEffluent_1_SS());
                    report_Effluent_1_PH.setText(model.getEffluent_1_PH());
                    report_Effluent_2_COD.setText(model.getEffluent_2_COD());
                    report_Effluent_2_NH3.setText(model.getEffluent_2_NH3());
                    report_Effluent_2_TN.setText(model.getEffluent_2_TN());
                    report_Effluent_2_TP.setText(model.getEffluent_2_TP());
                    report_Effluent_2_SS.setText(model.getEffluent_2_SS());
                    report_Effluent_2_PH.setText(model.getEffluent_2_PH());
                    report_Effluent_1_Mud.setText(model.getEffluent_1_Mud());
                    report_Effluent_1_MudWater.setText(model.getEffluent_1_MudWater());
                    report_Effluent_2_Mud.setText(model.getEffluent_2_Mud());
                    report_Effluent_2_MudWater.setText(model.getEffluent_2_MudWater());
                    report_Creator.setText(model.getCreator());
                    report_CreateDT.setText(MyTextUtils.toYMD(model.getCreateDT()));
                    report_Memo.setText(model.getMemo());

                    //余氯
                    InletYL_ET.setText(model.getInletYL());
                    Effluent_1_YL_ET.setText(model.getEffluent_1_YL());
                } else {
                    ToastShow(msg);
                }

                dialog.close();
            }
        });

    }


    private void initSubmit() {
        String HandleAmount1 = report_HandleAmount1.getText().toString();
        String HandleAmount2 = report_HandleAmount2.getText().toString();

        String PumpLevel0 = report_PumpLevel0.getText().toString();
        String PumpLevel4 = report_PumpLevel4.getText().toString();
        String PumpLevel8 = report_PumpLevel8.getText().toString();
        String PumpLevel12 = report_PumpLevel12.getText().toString();
        String PumpLevel16 = report_PumpLevel16.getText().toString();
        String PumpLevel20 = report_PumpLevel20.getText().toString();

        String InletCOD = report_InletCOD.getText().toString();
        String InletNH3 = report_InletNH3.getText().toString();
        String InletTN = report_InletTN.getText().toString();
        String InletTP = report_InletTP.getText().toString();
        String InletSS = report_InletSS.getText().toString();
        String InletPH = report_InletPH.getText().toString();

        String Effluent_1_COD = report_Effluent_1_COD.getText().toString();
        String Effluent_1_NH3 = report_Effluent_1_NH3.getText().toString();
        String Effluent_1_TN = report_Effluent_1_TN.getText().toString();
        String Effluent_1_TP = report_Effluent_1_TP.getText().toString();
        String Effluent_1_SS = report_Effluent_1_SS.getText().toString();
        String Effluent_1_PH = report_Effluent_1_PH.getText().toString();

        String Effluent_2_COD = report_Effluent_2_COD.getText().toString();
        String Effluent_2_NH3 = report_Effluent_2_NH3.getText().toString();
        String Effluent_2_TN = report_Effluent_2_TN.getText().toString();
        String Effluent_2_TP = report_Effluent_2_TP.getText().toString();
        String Effluent_2_SS = report_Effluent_2_SS.getText().toString();
        String Effluent_2_PH = report_Effluent_2_PH.getText().toString();

        String Effluent_1_Mud = report_Effluent_1_Mud.getText().toString();
        String Effluent_1_MudWater = report_Effluent_1_MudWater.getText().toString();
        String Effluent_2_Mud = report_Effluent_2_Mud.getText().toString();
        String Effluent_2_MudWater = report_Effluent_2_MudWater.getText().toString();

        String InletYL = InletYL_ET.getText().toString();
        String Effluent_1_YL = Effluent_1_YL_ET.getText().toString();

        String Creator = report_Creator.getText().toString();
        String CreateDT = report_CreateDT.getText().toString();

        String Memo = report_Memo.getText().toString();
//        if(MyTextUtils.IsNullByString(HandleAmount1) || MyTextUtils.IsNullByString(HandleAmount2)
//                || MyTextUtils.IsNullByString(PumpLevel0)  || MyTextUtils.IsNullByString(PumpLevel4)
//                || MyTextUtils.IsNullByString(PumpLevel8)  || MyTextUtils.IsNullByString(PumpLevel12)
//                || MyTextUtils.IsNullByString(PumpLevel16)  || MyTextUtils.IsNullByString(PumpLevel20)
//                || MyTextUtils.IsNullByString(InletCOD)  || MyTextUtils.IsNullByString(InletNH3)
//                || MyTextUtils.IsNullByString(InletTN)  || MyTextUtils.IsNullByString(InletTP)
//                || MyTextUtils.IsNullByString(InletSS)  || MyTextUtils.IsNullByString(InletPH)
//                || MyTextUtils.IsNullByString(Effluent_1_COD)  || MyTextUtils.IsNullByString(Effluent_2_COD)
//                || MyTextUtils.IsNullByString(Effluent_1_NH3)  || MyTextUtils.IsNullByString(Effluent_2_NH3)
//                || MyTextUtils.IsNullByString(Effluent_1_TN)  || MyTextUtils.IsNullByString(Effluent_2_TN)
//                || MyTextUtils.IsNullByString(Effluent_1_TP)  || MyTextUtils.IsNullByString(Effluent_2_TP)
//                || MyTextUtils.IsNullByString(Effluent_1_SS)  || MyTextUtils.IsNullByString(Effluent_2_SS)
//                || MyTextUtils.IsNullByString(Effluent_1_PH)  || MyTextUtils.IsNullByString(Effluent_2_PH)
//                || MyTextUtils.IsNullByString(Effluent_1_Mud)  || MyTextUtils.IsNullByString(Effluent_1_MudWater)
//                || MyTextUtils.IsNullByString(Effluent_2_Mud)  || MyTextUtils.IsNullByString(Effluent_2_MudWater)
//                ){
//            ToastShow("请填写所有的指标数据！");
//            return;
//        }


        HashMap<String, Object> maps = new HashMap<>();
        if (CacheData.PurificationPlantDay_UPDATE) {
            maps.put("ID", model.getID());
        }
        maps.put("HandleAmount1", HandleAmount1);
        maps.put("HandleAmount2", HandleAmount2);
        maps.put("PumpLevel0", PumpLevel0.equals("")?null:PumpLevel0);
        maps.put("PumpLevel4", PumpLevel4.equals("")?null:PumpLevel4);
        maps.put("PumpLevel8", PumpLevel8.equals("")?null:PumpLevel8);
        maps.put("PumpLevel12", PumpLevel12.equals("")?null:PumpLevel12);
        maps.put("PumpLevel16", PumpLevel16.equals("")?null:PumpLevel16);
        maps.put("PumpLevel20", PumpLevel20.equals("")?null:PumpLevel20);

        maps.put("InletCOD", InletCOD);
        maps.put("InletNH3", InletNH3);
        maps.put("InletTN", InletTN);
        maps.put("InletTP", InletTP);
        maps.put("InletSS", InletSS);
        maps.put("InletPH", InletPH);


        maps.put("Effluent_1_COD", Effluent_1_COD);
        maps.put("Effluent_1_NH3", Effluent_1_NH3);
        maps.put("Effluent_1_TN", Effluent_1_TN);
        maps.put("Effluent_1_TP", Effluent_1_TP);
        maps.put("Effluent_1_SS", Effluent_1_SS);
        maps.put("Effluent_1_PH", Effluent_1_PH);

        maps.put("Effluent_2_COD", Effluent_2_COD);
        maps.put("Effluent_2_NH3", Effluent_2_NH3);
        maps.put("Effluent_2_TN", Effluent_2_TN);
        maps.put("Effluent_2_TP", Effluent_2_TP);
        maps.put("Effluent_2_SS", Effluent_2_SS);
        maps.put("Effluent_2_PH", Effluent_2_PH);


        maps.put("Effluent_1_Mud", Effluent_1_Mud);
        maps.put("Effluent_1_MudWater", Effluent_1_MudWater);
        maps.put("Effluent_2_Mud", Effluent_2_Mud);
        maps.put("Effluent_2_MudWater", Effluent_2_MudWater);
        maps.put("Memo", Memo);
        maps.put("Creator", Creator);
        maps.put("CreateDT", CreateDT);

        maps.put("InletYL", InletYL);
        maps.put("Effluent_1_YL", Effluent_1_YL);

        dialog = new LoadingDialog(PurificationPlantDayEditActivity.this, "加载中...");
        dialog.show();
        String url = Config.host + WebApi.Api_PostPurificationPlantDay;
        Log.d("请求曲线", "新增查询:" + url);
        DataModel.PostApiRequest(mContext, url, maps, new InitDataCallBack() {

            @Override
            public void callbak(boolean b, String string) {
                if (b) {
                    Intent intent_report = new Intent(PurificationPlantDayEditActivity.this, PurificationPlantDayReadActivity.class);//报表录入
                    intent_report.putExtra("ID", model.getID());
                    CacheData.PurificationPlantReadDay_UPDATE = true;
                    startActivity(intent_report);
                    Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
                    finish();
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                } else {
                    Toast.makeText(mContext, "保存失败" + string, Toast.LENGTH_SHORT).show();
                }
                dialog.close();
            }
        });
    }


    private void chooseTime() {
        TimePickerView.Builder builder = new TimePickerView.Builder(mContext, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
                String time = MyTextUtils.date2String(date, simpleDateFormat);
                report_CreateDT.setText(time);
            }
        });
        builder.setType(new boolean[]{true, true, true, false, false, false});
        TimePickerView pvTime = builder.build();
        pvTime.setDate(Calendar.getInstance());
        pvTime.show();
    }

    //退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            return super.onKeyDown(keyCode, event);
        }else{
            return  false;
        }
    }


}
